package com.bupt.vueblog.controller;

import com.bupt.vueblog.constant.Result;
import com.bupt.vueblog.constant.Status;
import com.bupt.vueblog.domain.dto.DetailInfoDTO;
import com.bupt.vueblog.domain.entity.Comment;
import com.bupt.vueblog.domain.dto.CommentDTO;
import com.bupt.vueblog.domain.entity.Relation;
import com.bupt.vueblog.domain.vo.BriefCommentVO;
import com.bupt.vueblog.domain.vo.CommentVO;
import com.bupt.vueblog.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 谭继好
 * @Description
 * @date 2024-04-10 16:15
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/article/{userId}")
    // 查询用户发表所有博客下的评论
    public Result<BriefCommentVO> getByUserArticles(@PathVariable int userId) {
        List<BriefCommentVO> res = commentService.getByUserArticles(userId);
        return Result.success(res);
    }

    @GetMapping("/{userId}/{articleId}")
    // 查询博客下所有评论，用于修改和添加评论后的刷新（只刷新评论部分）
    public Result<CommentVO> getByArticleId(@PathVariable int userId, @PathVariable int articleId) {
        List<CommentVO> res = commentService.getByArticle(userId, articleId);
        return Result.success(res);
    }

    @GetMapping("/sub/{userId}")
    // 查询用户评论的子评论
    public Result<BriefCommentVO> getUserSubComments(@PathVariable int userId) {
        List<BriefCommentVO> res = commentService.getByUserComments(userId);
        return Result.success(res);
    }

    @GetMapping("/{userId}")
    // 查询某用户所有评论
    public Result<BriefCommentVO> getUserComments(@PathVariable int userId) {
        List<BriefCommentVO> res = commentService.getByUser(userId);
        return Result.success(res);
    }

    @PostMapping
    // 前端只传过来必要的值，后端补全
    public Result addComment(@RequestBody CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setLikeNum(0);
        comment.setUpdateTime(LocalDateTime.now());
        commentService.insert(comment);
        return Result.success();
    }

    @PutMapping()
    // 前端只传过来id和内容，不用带更新时间，后端赋值
    public Result changeDetail(@RequestBody DetailInfoDTO data) {
        data.setUpdateTime(LocalDateTime.now());
        commentService.changeInfo(data);
        return Result.success();
    }

    // 添加与取消点赞需要分开写，修改关系表与修改数量事务在service层控制

    @PutMapping("/addNum")
    public Result addNum(@RequestBody Relation data) {
        data.setType(Status.COMMENT_LIKE);
        commentService.addNum(data);
        return Result.success();
    }

    @PutMapping("/subNum")
    public Result subNum(@RequestBody Relation data) {
        data.setType(Status.COMMENT_LIKE);
        commentService.subNum(data);
        return Result.success();
    }

    @DeleteMapping("/{id}/{articleId}")
    // 删除的时候不仅要传入评论Id，还要传入博客Id，便于更新评论数量
    public Result delete(@PathVariable int id, @PathVariable("articleId") int articleId) {
        commentService.delete(id, articleId);
        return Result.success();
    }

}
